package com.ctgu.frequence.step0;

import java.io.IOException;
import java.util.Arrays;

public class CountLetter {
    public static void countLetter(String str) throws IOException {
        int sum = 0;
        Letter[] letters = new Letter[26];
        for (int i = 0; i < 26; i++) {
            letters[i] = new Letter();
            letters[i].index = i;
            letters[i].num = 0;
        }
        String ex = "abcdefghijklmnopqrstuvwxyz";
        for (int i = 0; i < str.length(); i++) {
            for (int j = 0; j < ex.length(); j++) {
                if(str.charAt(i) == ex.charAt(j) || str.charAt(i) == ex.charAt(j) - 32){//处理大小写
                    letters[ex.charAt(j) - 'a'].num++;
                    sum++;
                    break;
                }
            }
        }
        Arrays.sort(letters);
        for (int i = 0; i < 26; i++) {
            double per = 1.0*letters[i].num / sum * 100;
            System.out.printf("%c的出现频率为%.2f%%\n",letters[i].index + 'a',per);
        }
    }
}
